package jtk.project4.fleet.screen.workOrderManagement;

import java.awt.FlowLayout;
import java.util.ArrayList;

import jtk.project4.fleet.action.CloseAction;
import jtk.project4.fleet.domain.History;
import jtk.project4.fleet.event.WorkOrderManagementEvent;
import jtk.project4.fleet.event.WorkOrderManagementEvent.WorkOrderManagementEventType;
import jtk.project4.fleet.field.ViewOptionsField;
import jtk.project4.fleet.field.WorkOrderManagementField;


import nl.coderight.jazz.action.Action;
import nl.coderight.jazz.action.ActionGroup;
import nl.coderight.jazz.dialog.MessageType;
import nl.coderight.jazz.dialog.OptionDialog;
import nl.coderight.jazz.dialog.OptionType;
import nl.coderight.jazz.dialog.ReturnOptionType;
import nl.coderight.jazz.form.FormHeader;
import nl.coderight.jazz.form.FormLayout;
import nl.coderight.jazz.form.FormView;
import nl.coderight.jazz.form.control.GroupControl;
import nl.coderight.jazz.form.field.SearchField;
import nl.coderight.jazz.form.field.TableField;
import nl.coderight.jazz.form.field.TableField.AutoResizeMode;
import nl.coderight.jazz.form.field.button.PushButton;
import nl.coderight.jazz.i18n.Message;
import nl.coderight.jazz.i18n.Message.Choice;
import nl.coderight.jazz.i18n.Message.Param;
import nl.coderight.jazz.view.StatusBar;

public class WorkOrderManagementView extends FormView{
	
	private TableField<History> workOrderManagementTable;
	private TableField<History> filterHistoryField;
	
	private Action closeAction;
	
	private Action addHistoryAction;
	private Action editHistoryAction;
	private Action deleteHistoryAction;
	private ActionGroup editHistoryActionGroup;
	
	private Message status;
	private StatusBar statusBar;
	
	private PushButton closeButton;
	private PushButton toggleStatusButton;
	private FormHeader formHeader;
	private PushButton helpButton;
	private PushButton addButton;
	private PushButton editButton;
	private PushButton deleteButton;
	private SearchField filterField;
	
	
	public void handleEvent (WorkOrderManagementEvent evt){
		History selectedHistory = workOrderManagementTable.getValue();
		switch(evt.getType()){
		case CLOSE:
			close();
		case CHANGE:
			//bwt filter
		case SELECT :
			ArrayList<Choice> choices = new ArrayList<Choice>();
			choices.add(new Choice(0, "status.message.none"));
			choices.add(new Choice(1, "status.message.one"));
			choices.add(new Choice(2, "status.message.all"));
			
			status = new Message("status.message");
			status.addParam(new Param(workOrderManagementTable.getValueCount()));
			status.addParam(new Param(workOrderManagementTable.getListOfValues().size(), choices));
			status.addParam(new Param(workOrderManagementTable.getSelectionCount()));
			// display message on status bar
			statusBar = new StatusBar();
			statusBar.setMessage(status);
	

		break;
		case DELETE:
			Message message = new Message("dialog.delete.message");
			message.addParam(new Param(selectedHistory.getComments()));
			message.addParam(new Param(selectedHistory.getStatus()));
			
			OptionDialog dialog = new OptionDialog(this);
			dialog.setTitle("dialog.delete.title");
			dialog.setOptionType(OptionType.YES_NO);
			dialog.setOptions("button.yes", "button.no", "button.cancel");
			dialog.setDefaultOption("button.no");
			dialog.setMessageType(MessageType.QUESTION);
			dialog.setMessage(message);
			dialog.show();			
			if (dialog.getReturnOptionType() == ReturnOptionType.YES) {
				propagateEvent(evt);
			}
			break;
		case FILTER:
			// filterField is changed, update filter
			//historyFilter.setValue(filterField.getValue());
			break;
			
		default:
			// propagate event to controller
			propagateEvent(evt);
			break;
		

		}
	}
	@Override
	protected void init() {
		createFields();
		createLayout();
		createActions();
	}
	
	private void createActions() {
		// TODO Auto-generated method stub
		
		closeAction = new CloseAction();
		
	}
	
	private GroupControl createButtons() {
		GroupControl buttons = new GroupControl();
		buttons.setLayout(new FlowLayout(FlowLayout.RIGHT));
		buttons.addField(closeButton);
		buttons.addField(toggleStatusButton);
		return buttons;
	}
	
	private GroupControl createForm() {
		GroupControl form = new GroupControl();
		form.addField(new WorkOrderManagementField("Work Order Management"), NORTH);
		form.addField(new ViewOptionsField("View Options"),CENTER);
		form.addField(createButtons(), SOUTH);		
		return form;
	
	
	}
	
	private void createLayout() {
		addField(createGroupFields(),CENTER);
		addField(createButtons(),SOUTH);
		setResizable(false);
		
	}

	private FormHeader createHeader() {
		formHeader = new FormHeader();
		formHeader.setTitle("Work Order Management.header.form");
		return formHeader;
	}
	
	private GroupControl createGroupFields(){
		GroupControl field = new GroupControl();
		setLayout(new FormLayout())
		.addField(workOrderManagementTable)
		.addRow()
		.addField(new ViewOptionsField("History"))
		;
		
		return field;
	}
	
	
	private void createFields(){
		closeButton = new PushButton("closeAction");
		toggleStatusButton = new PushButton("toggleStatusAction");
		
		workOrderManagementTable = new TableField<History>("historys","history");
		workOrderManagementTable.setTitle("workOrderManagement.header.table");
		workOrderManagementTable.setAutoResizeMode(AutoResizeMode.OFF);
		workOrderManagementTable.getSelectionValues();
		
		workOrderManagementTable.setOnSelectEvent(new WorkOrderManagementEvent(WorkOrderManagementEventType.SELECT));

		
		workOrderManagementTable.addColumn("label.Work Order","woNo");
		workOrderManagementTable.addColumn("label.Identification","dateS");
		workOrderManagementTable.addColumn("label.Date Issued","dateI");
		workOrderManagementTable.addColumn("label.Status","status");
		workOrderManagementTable.addColumn("label.Date Complete","dateC");
		workOrderManagementTable.addColumn("label.Down Time (Days)","downTime");
		workOrderManagementTable.addColumn("label.Meter","units");
		workOrderManagementTable.addColumn("label.Priority","priority");
		workOrderManagementTable.addColumn("label.Type","type");
		
		
		workOrderManagementTable.addColumn("label.Auto Tax","autoTax");
		workOrderManagementTable.addColumn("label.Employee","empId");
		workOrderManagementTable.addColumn("label.Vendor","vendorId");
		workOrderManagementTable.addColumn("label.Invoice#","invNo");
		workOrderManagementTable.addColumn("label.PO#","po");
		workOrderManagementTable.addColumn("label.Cost Center","costCenter");
		workOrderManagementTable.addColumn("PM Cost","pmCost");
		workOrderManagementTable.addColumn("label.Repair Cost"," rCost");
		workOrderManagementTable.addColumn("label.Part Cost","pCost");
		workOrderManagementTable.addColumn("label.Labor Cost","lCost");
		workOrderManagementTable.addColumn("label.Taxes","taxes");
		workOrderManagementTable.addColumn("label.Total Cost","tCost");
		workOrderManagementTable.addColumn("label.Undefined Cost","udfCost");
		workOrderManagementTable.addColumn("label.Discount %","discPct");
		workOrderManagementTable.addColumn("label.Discount Value","discVal");
		workOrderManagementTable.addColumn("label.Location","locId");
		workOrderManagementTable.addColumn("label.Category","category");
		workOrderManagementTable.addColumn("label.UDF1Val1#","udfVal1");
		workOrderManagementTable.addColumn("label.UDF2Val2#","udfVal2");
		workOrderManagementTable.addColumn("label.Department","department");

		
	}
	
	
	
}

